package com.hlkj.baojin.utils;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * 日志定义工具
 *
 * @author lzx
 **/
public class LogUtils {

    private String system = "King";

    private String module;

    private String msg;

    private LinkedHashMap<String, Object> args = new LinkedHashMap();

    public LogUtils(String module) {
        this.module = module;
    }

    public static LogUtils module(String module) {
        return new LogUtils(module);
    }

    public LogUtils msg(String msg) {
        this.msg = msg;
        return this;
    }

    public LogUtils args(String key, Object value) {
        args.put(key, value);
        return this;
    }

    public String build() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append(this.system);
        sb.append("|");
        sb.append(this.module);
        sb.append("]");
        sb.append("  ");
        sb.append(this.msg);
        sb.append("  ");
        sb.append("[");
        buildMap(sb);
        sb.append("]");
        return sb.toString();
    }

    private StringBuilder buildMap(StringBuilder sb) {
        Set<String> keys = this.args.keySet();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String value = this.args.get(key).toString();
            sb.append(key);
            sb.append("=");
            sb.append(value);
            if (iterator.hasNext()) {
                sb.append("  ");
            }
        }
        return sb;
    }

}
